'use strict';
import { watch } from "vue"

// 获取参数名
function getParameterNames(fn: Function) {
    if (typeof fn !== 'function') return [];
    var COMMENTS = /((\/\/.*$)|(\/\*[\s\S]*?\*\/))/mg;
    var code = fn.toString().replace(COMMENTS, '');
    var result = code.slice(code.indexOf('(') + 1, code.indexOf(')'))
        .match(/([^\s,]+)/g);
    return result === null
        ? []
        : result;
}

// 重组
export function map(tree: any, match: any) {
    for (let i = 0; i < tree.length; i++) {
        const item = tree[i];
        if (item.lc_template) {
            let obj = match.find((i: any) => i.key == item.lc_template);
            // 删除节点，重新填充，或不处理
            tree.splice(i, 1);

            if (obj) {
                tree.splice(i, 0, obj);
            }
        }
        if (item.slot) {
            map(item.slot, match);
        }

        if (item.children) {
            map(item.children, match);
        }
    }
}

// 打补丁
export function insertTree(tree: any, match: any) {
    map(tree, match);

    return tree;
}

// 分割
export function splitKey(key: string): string[] {
    return key.split(".");
}

// 处理item
export function handleMethodsV1(item: any, props: any, cb?: Function) {
    if (!item.attributes) {
        item.attributes = {};
    }
    const attributes = item.attributes;

    if (item.methods) {
        Object.keys(item.methods).forEach((key) => {
            attributes[key] = new Function(item.methods[key]);
        });
    }

    if (item.tag.includes("table")) {
        if (item.attributes.dataSource) {
            watch(
                () => props.golbal["dataSource"],
                (val) => {
                    item.attributes.dataSource = props.golbal["dataSource"];
                }
            );
        }

        if (item.attributes.columns) {
            watch(
                () => props.golbal["columns"],
                (val) => {
                    item.attributes.columns = props.golbal["columns"];
                }
            );
        }
    }

    // 封装过滤器
    if (["ml-search-basic"].includes(item.tag)) {
        if (!props.golbal[attributes.modelKey]) {
            props.golbal[attributes.modelKey] = {};
        }

        attributes.form = props.golbal[attributes.modelKey];
        attributes.children = item.children;
    }

    if (item.ref) {
        item.attributes['ref'] = item.ref
    }

    return item;
}

// 函数处理
export function rewriteHookV1(item: any, golbal: any) {

    const handleEvent = (event: any, cb?: Function) => {
        if (!event) {
            return null
        }

        Object.keys(event).forEach(k => {
            let str = event[k]

            let func = golbal.event.find((item: any) => item.name === str)?.func || '';

            if (cb) {
                cb(k, func);
            } else {
                if (!item.attributes) {
                    item.attributes = {};
                }
                
                item.attributes[k] = function () {
                    const func = new Function(...arguments);
                    func.golbal = golbal;
                }
            }

        })
    }


    if (item.attributes.actions) {
        item.attributes.actions.forEach((action: any) => {
            action.event && handleEvent(action.event, (k: string, func: string) => {
                action.event[k] = new Function('row', func).bind(golbal)
            })
        })
    }

    handleEvent(item.event);

    return item
}